package com.example.mybatisplusdemo.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.Methods.methods;
import com.example.mybatisplusdemo.Result.Result;
import com.example.mybatisplusdemo.Service.AdminService;
import com.example.mybatisplusdemo.Service.BusinessService;
import com.example.mybatisplusdemo.Service.CateService;
import com.example.mybatisplusdemo.Service.UserService;
import com.example.mybatisplusdemo.pojo.Cate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class CateController {
    @Resource
    CateService cateService;
    @Resource
    AdminService adminService;
    @Resource
    BusinessService businessService;
    @Resource
    UserService userService;

    /**
     * 管理员获取分类方法
     * @param token
     * @return
     */
    @GetMapping("/admingetcate")
    public Result admingetcate(@RequestParam("token") String token){
        if (!adminService.checktoken(token)){
            return new Result(400,"无权限");
        }
        List<Cate> list = cateService.list(new QueryWrapper<Cate>().orderByDesc("id"));
        return new Result(200,"获取成功",list);
    }

    /**
     * 模糊查找
     * @param token
     * @param word
     * @return
     */
    @GetMapping("/admingetcatebyname")
    public Result admingetcatebyname(@RequestParam("token") String token,
                                     @RequestParam("words") String word){
        if (!adminService.checktoken(token)){
            return new Result(400,"无权限");
        }
        List<Cate> list = cateService.list(new QueryWrapper<Cate>().like("catename",word).orderByDesc("id"));
        return new Result(200,"查询成功",list);
    }

    /**
     * 模糊查找
     * @param token
     * @param word
     * @return
     */
    @GetMapping("/busgetcatebyname")
    public Result busgetcatebyname(@RequestParam("token") String token,
                                     @RequestParam("words") String word){
        if (!businessService.checktoken(token)){
            return new Result(400,"无权限");
        }
        List<Cate> list = cateService.list(new QueryWrapper<Cate>().like("catename",word).orderByDesc("id"));
        return new Result(200,"查询成功",list);
    }

    /**
     * 商家获取分类方法
     * @param token
     * @return
     */
    @GetMapping("/busgetcate")
    public Result busgetcate(@RequestParam("token") String token){
        if (!businessService.checktoken(token)){
            return new Result(400,"无权限");
        }
        List<Cate> list = cateService.list(new QueryWrapper<Cate>().orderByDesc("id"));
        return new Result(200,"获取成功",list);
    }

    /**
     * 管理员添加分类方法
     * @param token
     * @param catename
     * @return
     */
    @PostMapping("/addcate")
    public Result addcate(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                          @Valid @NotBlank(message = "分类不能为空")@RequestParam("catename") String catename){
        if (!adminService.checktoken(token)){
            return new Result(400,"无权限");
        }
        int count = cateService.count(new QueryWrapper<Cate>().eq("catename",catename));
        if (count !=0){return new Result(400,"分类已经存在");}
        Cate cate = new Cate();
        cate.setCatename(catename);
        boolean isSave = cateService.save(cate);
        if (isSave){return new Result(200,"添加成功");}
        else{return new Result(400,"添加失败");}
    }

    /**
     * 分类更新方法
     * @param token
     * @param catename
     * @param newcatename
     * @return
     */
    @PostMapping("/updatecate")
    public Result updatecate(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                             @Valid @NotBlank(message = "分类不能为空")@RequestParam("catename") String catename,
                             @Valid @NotBlank(message = "新的分类不能为空")@RequestParam("newcatename") String newcatename) {
        if (!adminService.checktoken(token)){
            return new Result(400,"无权限");
        }
        if  (!new methods().checklenth(newcatename,1,10)) {return new Result(400,"分类长度不正确");}
        int count = cateService.count(new QueryWrapper<Cate>().eq("catename",newcatename));
        if (count != 0){return new Result(400,"分类已经存在！");}
        Cate cate = cateService.getOne(new QueryWrapper<Cate>().eq("catename",catename));
        cate.setCatename(newcatename);
        boolean isUpdata = cateService.update(cate,new QueryWrapper<Cate>().eq("catename",catename));
        if (isUpdata){return new Result(200,"更新成功");}
        else{return new Result(400,"更新失败");}
    }

    /**
     * 分类删除方法
     * @param token
     * @param id
     * @return
     */
    @PostMapping("/deletecate")
    public Result deletecate(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                             @Valid @NotBlank(message = "分类id不能为空")@RequestParam("id") String id){
        if (!adminService.checktoken(token)){
            return new Result(400,"无权限");
        }
        Cate cate = cateService.getOne(new QueryWrapper<Cate>().eq("id",id));
        if (Integer.parseInt(cate.getNum())!= 0){return new Result(400,"该分类下有物品");}
        boolean isDelete = cateService.remove(new QueryWrapper<Cate>().eq("id",id));
        if (isDelete){return new Result(200,"删除成功");}
        else{return new Result(400,"删除失败");}
    }


    /**
     * 用户购物车商品数量变化
     * @param token
     * @param catename
     * @param num
     * @return
     */
    @PostMapping("/setgoodsnum")
    public Result setgoodsnum(@Valid @NotBlank(message = "token不能为空")@RequestParam("token") String token,
                              @Valid @NotBlank(message = "分类不能为空")@RequestParam("catename") String catename,
                              @Valid @NotBlank(message = "数量不能为空")@RequestParam("num") Integer num){
        if (!userService.checktoken(token)){
            return new Result(400,"无权限");
        }
        Cate cate = cateService.getOne(new QueryWrapper<Cate>().eq("catename",catename));
        if (num > 10 || num < 0 ){return new Result(400,"数量越界");}
        cate.setNum(String.valueOf(num));
        boolean isUpdate = cateService.update(cate,new QueryWrapper<Cate>().eq("catename",catename));
        if (isUpdate){return new Result(200,"set num OK");}
        else{return new Result(400,"set num ERR");}
    }

    /**
     * 获取cates
     * @return
     */
    @GetMapping("/getcates")
    public Result getcates(){
        return new Result(200,"获取成功",cateService.list(new QueryWrapper<Cate>().orderByDesc("id")));
    }
}
